using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SeaBattleControl
{
    public partial class BattleshipSprite : UserControl
    {
        private Int16 mSize;
        private Point mInitialCursorPos;
        private Int32 mXOffset, mYOffset;
        private SeaBattleMap mParentMap;
        private Int16 mID;

        public Int16 ID
        {
            get { return mID; }
        }
                
        public BattleshipSprite(Int16 id, Int16 size, SeaBattleMap parentMap)
        {
            InitializeComponent();
            mSize = size;
            mParentMap = parentMap;
            mID = id;
        }

        private void BattleshipSprite_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int tmpWidth = this.Width;
                int tmpHeight = this.Height;

                this.Width = tmpHeight;
                this.Height = tmpWidth;

                this.Invalidate();
            }
        }

        private void BattleshipSprite_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 1)
            {
                calculateXYCoords(e.X, e.Y);

                BattleshipSprite battleship = (BattleshipSprite)sender;
                this.DoDragDrop(battleship, DragDropEffects.Move);
            }
        }

        private void BattleshipSprite_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            int Ypos = MousePosition.Y - mYOffset;
            int Xpos = MousePosition.X - mXOffset;

            Xpos -= mInitialCursorPos.X;
            Ypos -= mInitialCursorPos.Y;

            Xpos -= (Xpos % mSize);
            Ypos -= (Ypos % mSize);

            if (this.Left != Xpos || this.Top != Ypos)
            {

                SeaBattleMap.ePosValidationResult result = mParentMap.validatePosition(mID, Xpos, Ypos, Width, Height);

                if (result == SeaBattleMap.ePosValidationResult.PosVal_Correct)
                {
                    this.Left = Xpos;
                    this.Top = Ypos;

                    BackColor = System.Drawing.Color.Black;
                }
                else if (result == SeaBattleMap.ePosValidationResult.PosVal_ObjectCollision)
                {
                    this.Left = Xpos;
                    this.Top = Ypos;

                    BackColor = System.Drawing.Color.Red;
                }
                else
                {
                    Console.WriteLine("Bounds overlapped");
                }
            }
        }

        private void calculateXYCoords(int x, int y)
        {
            mXOffset = Parent.Parent.Left + Parent.Left + SystemInformation.FrameBorderSize.Width;
            mYOffset = Parent.Parent.Top + Parent.Top + SystemInformation.CaptionHeight + SystemInformation.FrameBorderSize.Height;

            Point screen = PointToScreen(new Point(x, y));
            mInitialCursorPos = PointToClient(screen);
        }
    }
}
